package com.sam.chat.controller;

import com.sam.chat.api.CommonResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * <p>
 * 文件管理
 * </p>
 *
 * @author sam
 * @since 2022-12-31
 */
@Tag(name = "文件管理")
@RestController
@RequestMapping("/file")
@RequiredArgsConstructor
@Component
public class FileController {
    private static final String dirPath = "/sam/download/chat/";
    private static final String DOWNLOAD_URL = "https://www.godboy.cn/download/chat/";

    /**
     * 注册上传头像
     */
    @Operation(summary = "注册上传头像")
    @PostMapping("/uploadHeader")
    public CommonResult<String> uploadHeader(@RequestParam(value = "imageFile") MultipartFile imageFile) {
        if (imageFile == null) {
            return new CommonResult<>(201, "文件上传失败", "");
        }
        //获取上传的图片格式
        String originalFilename = imageFile.getOriginalFilename();
        String type = "";
        if (originalFilename.contains(".")) {
            type = originalFilename.substring(originalFilename.lastIndexOf("."));
        } else {
            type = "";
        }
        //重命名文件,新建header的文件夹
        String fileName = UUID.randomUUID().toString();
        File targetFile = new File(dirPath + "header/", fileName + type);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        } else {
            targetFile.delete();
        }
        //转存到centos
        try {
            imageFile.transferTo(targetFile);
            return new CommonResult<>(200, "success", DOWNLOAD_URL + "header/" + fileName + type);
        } catch (IllegalStateException | IOException e) {
            e.printStackTrace();
        }

        return new CommonResult<>(201, "文件上传失败", "");
    }

    /**
     * 上传各种聊天文件
     */
    @Operation(summary = "上传各种聊天文件")
    @PostMapping("/uploadFile")
    public CommonResult<String> uploadFile(@RequestParam(value = "file") MultipartFile imageFile) {
        if (imageFile == null) {
            return new CommonResult<>(201, "文件上传失败", "");
        }
        //获取上传的图片格式
        String originalFilename = imageFile.getOriginalFilename();
        String type = "";
        if (originalFilename.contains(".")) {
            type = originalFilename.substring(originalFilename.lastIndexOf("."));
        } else {
            type = "";
        }
        //重命名文件,新建header的文件夹
        String fileName = UUID.randomUUID().toString();
        File targetFile = new File(dirPath + "header/", fileName + type);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        } else {
            targetFile.delete();
        }
        //转存到centos
        try {
            imageFile.transferTo(targetFile);
            return new CommonResult<>(200, "success", DOWNLOAD_URL + "header/" + fileName + type);
        } catch (IllegalStateException | IOException e) {
            e.printStackTrace();
        }

        return new CommonResult<>(201, "文件上传失败", "");
    }
}
